#pragma once

#include "Thread.h"

#include <deque>
#include <vector>

class Task;
class ThreadPool {
public:
	ThreadPool();
	ThreadPool(const string &name);
	~ThreadPool();
	bool started() const {
		return _started;
	} 
	void setStarted(){_started = true;}

	bool threadsEmpty(){return _threads.empty();}
	void addThread(Thread *t){_threads.push_back(t);}
	void setThreadsNum(int n){_threadsNum = n;}
	int threadsNum(){return _threadsNum;}

	void mutexLock(){pthread_mutex_lock(&_mutex);}
	void mutexUnLock(){pthread_mutex_unlock(&_mutex);}

	virtual void start(int number);
	void stop();
	void clean();

	int addTask(Task * t);

	Thread *getOneThread();
	const string &name(){return _name;}
private:
	string _name;
	bool _started;

	vector <Thread *>_threads;
	int _threadsNum;

	pthread_cond_t _cond;
	pthread_mutex_t _mutex;

	Task *getNextTask();
	int funcInThread();
};

class TcpConnect;
class Server;
class TcpThreadPool : public ThreadPool {
public:
	TcpThreadPool(const string &name);

	int addTcpConnect(TcpConnect *tc);
	virtual void start(int number);

	static int threadsSerial;

	void setServer(Server *s){_server=s;}
	Server *getServer(){return _server;}
private:
	int funcInThread();

	Server *_server;

	Address _listenAddr[MAXTHREADSNUM];
	int _epfd[MAXTHREADSNUM];
	int _listenSock[MAXTHREADSNUM];
	struct epoll_event _epevents[MAXTHREADSNUM][1024];
	struct epoll_event _eplistenevent[MAXTHREADSNUM];
};



